package com.moli.iov.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moli.iov.sys.enums.EnVisibleEnum;
import com.moli.iov.sys.enums.PermissionStatusEnum;
import com.moli.iov.sys.enums.PermissionTypeEnum;
import com.moli.iov.sys.enums.UserTypeEnum;
import com.moli.iov.sys.mapper.PermissionMapper;
import com.moli.iov.sys.model.dto.req.PermissionDto;
import com.moli.iov.sys.model.dto.req.PermissionNeApiDto;
import com.moli.iov.sys.model.po.Permission;
import com.moli.iov.sys.service.IPermissionService;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 权限业务
 * </p>
 *
 * @author zql
 * @since 2019/9/6 15:13
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements
    IPermissionService {

  /**
   * 递归查询权限列表
   *
   * @param permissionDto 企业可见性 0：不可见，1：可见 权限详情
   */
  @Override
  public List<Permission> listPermission(PermissionDto permissionDto) {

    QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("pid", permissionDto.getPid());

    if (UserTypeEnum.COMPANY.getFlag() == permissionDto.getUserType()) {
      queryWrapper.eq("en_visible", EnVisibleEnum.ENABLED.getFlag());
    }
    queryWrapper.eq("status", PermissionStatusEnum.ENABLED.getFlag());
    queryWrapper.orderByAsc("sort");
    List<Permission> permissions = super.list(queryWrapper);
    if (permissions != null && !permissions.isEmpty()) {
      permissions.forEach(permission -> {
        permissionDto.setPid(permission.getPermissionId());
        permissionDto.setUserType(permissionDto.getUserType());
        permission.setChildren(listPermission(permissionDto));
      });
    }
    return permissions;
  }

  /**
   * 递归查询非api权限
   */
  @Override
  public List<Permission> listPermissionNeApi(PermissionNeApiDto permissionNeApiDto) {
    QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
    if (permissionNeApiDto.getPermissionIds() != null && !permissionNeApiDto.getPermissionIds()
        .isEmpty()) {
      queryWrapper.in("permission_id", permissionNeApiDto.getPermissionIds());
    }

    if (permissionNeApiDto.getPid() != null) {
      queryWrapper.eq("pid", permissionNeApiDto.getPid());
    }

    if(permissionNeApiDto.getEnVisible() != null){
      queryWrapper.eq("en_visible", permissionNeApiDto.getEnVisible());
    }
    queryWrapper.eq("status", PermissionStatusEnum.ENABLED.getFlag());
    queryWrapper.ne("permission_type", PermissionTypeEnum.API.getFlag());
    queryWrapper.orderByAsc("sort");

    List<Permission> permissions = super.list(queryWrapper);

    permissions.forEach(p -> {

      PermissionNeApiDto pnad = new PermissionNeApiDto();
      pnad.setPid(p.getPermissionId());
      pnad.setEnVisible(permissionNeApiDto.getEnVisible());
      pnad.setPermissionIds(permissionNeApiDto.getPermissionIds());

      p.setChildren(listPermissionNeApi(pnad));
    });

    return permissions;
  }


  /**
   * 查询权限列表，by 权限ids
   */
  @Override
  public List<Permission> listPermissionByIds(List<Long> permissionIds) {
    QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
    queryWrapper.in("permission_id", permissionIds);
    queryWrapper.eq("status", PermissionStatusEnum.ENABLED.getFlag());
    return super.list(queryWrapper);
  }

  /**
   * 根据权限ids集合 获取api权限: 包含按钮、Api
   * @param permissionIds
   * @return
   */
  @Override
  public List<Permission> listApiAndBtnPermissions(List<Long> permissionIds) {

    QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();

    queryWrapper.eq("permission_type", PermissionTypeEnum.API.getFlag());
    queryWrapper.eq("status", PermissionStatusEnum.ENABLED.getFlag());

    if(permissionIds != null && !permissionIds.isEmpty()){
      queryWrapper.or(i -> i.in("permission_id", permissionIds));
    }
    return super.list(queryWrapper);
  }

  /**
   * 获取所有的api权限: 包含按钮、Api
   * @return
   */
  @Override
  public List<Permission> listApiAndBtnPermissionsOfAll() {

    QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
    queryWrapper.isNotNull("api_url");
    queryWrapper.eq("status", PermissionStatusEnum.ENABLED.getFlag());

    return super.list(queryWrapper);
  }
}
